iT邦幫忙

2024 iThome 鐵人賽

DAY 8
0
AI/ ML & Data

從0開始的影像辨識之路系列 第 9

Tensorflow-python:語意分割-2-模型訓練(Day 8)

  • 分享至 

  • xImage
  •  

本次主題是以colab的環境進行學習的,在本篇文章中,我將講解影像辨識的基礎技能在接下來的文章中這些技能將多次出現,先讀過這些語法再繼續去看後面的文章會比較能快速上手喔。依照進度每個禮拜都會記錄不同的影像辨識方法,基本順序會從:

  1. OpenCV
  2. 圖片分類(Tensorflow-Image classification)
  3. 語意分割(Semantic Segmentation)
  4. 生成模仿圖片(CycleGAN and pix2pix in PyTorch)
  5. 物件辨識(tensorflow object detection)
  6. 額外分享(MediaPipe)

在我們上一篇文章中我們已經把資料集準備好了,接下來我們就回到colab裡面進行模型訓練吧。
雲端硬碟掛載:

from google.colab import drive
drive.mount('/content/drive')

模型訓練:
將雲端硬碟掛載好之後,我們就可以開始訓練模型了。後面文章會再補充模型的介紹以及模型的堆疊。在訓練好模型之後我們會將模型儲存到雲端硬碟,方便下次直接使用模型。

import os
import datetime
from PIL import Image
from IPython.display import display
import PIL
from PIL import ImageOps
import matplotlib.pyplot as plt
import cv2 as cv
import tensorflow as tf 
from tensorflow import keras
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Activation, ReLU
from tensorflow.keras.layers import BatchNormalization, Conv2DTranspose, Concatenate
from tensorflow.keras.models import Model, Sequential
import tensorflow as tf
import numpy as np
import tarfile
from matplotlib import gridspec
from keras.preprocessing.image import load_img
from IPython.display import clear_output
import matplotlib.pyplot as plt
from tensorflow.keras import layers
import matplotlib.image

input_dir = "/content/drive/MyDrive/Segmentation/images"
target_dir = "/content/drive/MyDrive/Segmentation/masks"
num_num_class = 5
batch_size = 1
img_size = (160, 160)

input_img_paths = sorted(
    [ 
        os.path.join(input_dir, fname)
        for fname in os.listdir(input_dir)
        if fname.endswith(".png")
    ]
)


target_img_paths = sorted(
    [
        os.path.join(target_dir, fname)
        for fname in os.listdir(target_dir)
        if fname.endswith(".png") and not fname.startswith(".")
    ]
)

print("樣本數:", len(input_img_paths))

for input_path, target_path in zip(input_img_paths[:10], target_img_paths[:10]):
    print(input_path, "|", target_path)

def convolution_operation(entered_input, filters=64):
    conv1 = Conv2D(filters, kernel_size = (3,3), padding = "same")(entered_input)
    batch_norm1 = BatchNormalization()(conv1)
    act1 = ReLU()(batch_norm1)
    conv2 = Conv2D(filters, kernel_size = (3,3), padding = "same")(act1)
    batch_norm2 = BatchNormalization()(conv2)
    act2 = ReLU()(batch_norm2)
    
    return act2

class data_read(keras.utils.Sequence):
    def __init__(self, batch_size, img_size, input_img_paths, target_img_paths):
        self.batch_size = batch_size
        self.img_size = img_size
        self.input_img_paths = input_img_paths
        self.target_img_paths = target_img_paths

    def __len__(self):
        return len(self.target_img_paths) // self.batch_size

    def __getitem__(self, idx):
        i = idx * self.batch_size
        batch_input_img_paths = self.input_img_paths[i : i + self.batch_size]
        batch_target_img_paths = self.target_img_paths[i : i + self.batch_size]
        x = np.zeros((batch_size,) + self.img_size + (3,), dtype="float32")
        for j, path in enumerate(batch_input_img_paths):
            img = load_img(path, target_size=self.img_size)
            x[j] = img
        y = np.zeros((batch_size,) + self.img_size + (1,), dtype="uint8")
        for j, path in enumerate(batch_target_img_paths):
            img = load_img(path, target_size=self.img_size, color_mode="grayscale")
            img = np.clip(img, 0, 9)
            y[j] = np.expand_dims(img, 2)
        return x, y

def decoder(entered_input, skip, filters=64):
    Upsample = Conv2DTranspose(filters, (2, 2), strides=2, padding="same")(entered_input)
    Connect_Skip = Concatenate()([Upsample, skip])
    out = convolution_operation(Connect_Skip, filters)
    return out

def encoder(entered_input, filters=64):
    enc1 = convolution_operation(entered_input, filters)
    MaxPool1 = MaxPooling2D(strides = (2,2))(enc1)
    return enc1, MaxPool1

def get_model(img_size):
    input1 = Input((160,160,3),batch_size=1)
    skip1, encoder_1 = encoder(input1, 8)
    skip2, encoder_2 = encoder(encoder_1, 8*2)
    skip3, encoder_3 = encoder(encoder_2, 8*4)
    skip4, encoder_4 = encoder(encoder_3, 8*8)
    conv_block = convolution_operation(encoder_4, 8*8)
    decoder_1 = decoder(conv_block, skip4, 8*8)
    decoder_2 = decoder(decoder_1, skip3, 8*4)
    decoder_3 = decoder(decoder_2, skip2, 8*2)
    decoder_4 = decoder(decoder_3, skip1, 8)
    out = Conv2D(10,1, padding="same", activation="sigmoid")(decoder_4)
    model = Model(input1, out)
    return model

keras.backend.clear_session()
model = get_model((160,160))
model.summary()
val_samples = 10
train_input_img_paths = input_img_paths
train_target_img_paths = target_img_paths
val_input_img_paths = input_img_paths
val_target_img_paths = target_img_paths

train_gen = data_read(
    1, img_size, train_input_img_paths, train_target_img_paths
)
val_gen = data_read(1, img_size, val_input_img_paths, val_target_img_paths)

tf.config.run_functions_eagerly(True)
model.compile(optimizer="nAdam", loss="sparse_categorical_crossentropy",
              metrics=['accuracy'])

class DisplayCallback(tf.keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs=None):
    clear_output(wait=True)
    show_predictions()
    print ('\nSample Prediction after epoch {}\n'.format(epoch+1))

log_dir = "/content/drive/MyDrive/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")

callbacks = [
    tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
]

log_dir = "/content/drive/MyDrive/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)


print(train_gen)
epochs = 10
model.fit(train_gen,validation_data=val_gen, epochs=epochs)
model.save('/content/drive/MyDrive/fit/model_1.h5')

訓練完成後成果圖:

如果訓練模型上遇到甚麼問題或是error的話歡迎丟到留言區討論喔!


文章主題一覽:

  1. OpenCV-python:影像辨識基礎技能-1(Day 1)
  2. OpenCV-python:影像辨識基礎技能-2(Day 2)
  3. OpenCV-python:影像辨識的基礎臉部偵測-加碼更新(Day 2)

  1. Tensorflow-python:圖片分類-1-資料集準備(Day 3)
  2. Tensorflow-python:圖片分類-2-模型訓練(Day 4)
  3. Tensorflow-python:圖片分類-3-模型實際使用(Day 5)
  4. Tensorflow-python:圖片分類-4-完整程式總結(Day 6)

  1. Tensorflow-python:語意分割-1-資料集介紹(Day 7)
  2. Tensorflow-python:語意分割-2-模型訓練(Day 8)
  3. Tensorflow-python:語意分割-3-模型實際使用(Day 9)
  4. Tensorflow-python:語意分割-4-完整程式總結(Day 10)

  1. CycleGAN-python:生成相似圖片「由簡化繁」-1-資料集介紹(Day 11)
  2. CycleGAN-python:生成相似圖片「由簡化繁」-2-模型訓練(Day 12)
  3. CycleGAN-python:生成相似圖片「由簡化繁」-3-模型實際使用(Day 13)
  4. CycleGAN-python:生成相似圖片「由簡化繁」-4-完整程式總結(Day 14)
  5. CycleGAN-python:生成相似圖片「由繁化簡」-1-資料集介紹(Day 15)
  6. CycleGAN-python:生成相似圖片「由繁化簡」-2-模型訓練(Day 16)
  7. CycleGAN-python:生成相似圖片「由繁化簡」-3-模型實際使用(Day 17)
  8. CycleGAN-python:生成相似圖片「由繁化簡」-4-完整程式總結(Day 18)

  1. tensorflow-object-detection:物件辨識-1-資料集介紹(Day 19)
  2. tensorflow-object-detection:物件辨識-2-模型訓練(Day 20)
  3. tensorflow-object-detection:物件辨識-3-模型實際使用(Day 21)
  4. tensorflow-object-detection:物件辨識-4-完整程式總結(Day 22)

  1. MediaPipe:額外分享-1-手部追蹤(Day 23)
  2. MediaPipe:額外分享-2-人臉檢測(Day 24)
  3. MediaPipe:額外分享-2-物體檢測(Day 25)

  1. Tensorflow-python:圖片分類-1-模型介紹(Day 26)
  2. Tensorflow-python:圖片分類-2-變形應用(Day 27)
  3. Tensorflow-python:語意分割-1-模型介紹(Day 28)
  4. Tensorflow-python:語意分割-2-變形應用(Day 29)
  5. CycleGAN-python:生成相似圖片-1-模型介紹(Day 30)
  6. CycleGAN-python:生成相似圖片-2-變形應用(Day 31)

粗體字為額外更新的文章。


上一篇
Tensorflow-python:語意分割-1-資料集介紹(Day 7)
下一篇
Tensorflow-python:語意分割-3-模型實際使用(Day 9)
系列文
從0開始的影像辨識之路31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言